<?php

namespace App\Admin\Controllers;

use App\Models\Member\Member;
use App\Models\Org;
use App\Models\Work;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Encore\Admin\Grid\Displayers\Actions;

class MemberController extends AdminController
{
    protected $title = '工作人员管理';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Member());


        $grid->name('姓名')->style('min-width:100px;');
        $grid->mobile('手机号')->style('min-width:100px;');
        $grid->column('org.title', '所属单位')->style('min-width:100px;');
        $grid->pos('职位')->style('min-width:100px;');
        $grid->column('status', '状态')->using(['0' => '禁用', '1' => '启用'], '未知')->dot([
            0 => 'danger',
            1 => 'success',
        ], 'warning')->sortable()->style('min-width:100px;');

        $grid->column('created_at', '创建时间')->sortable()->style('min-width:100px;');

        $grid->disableExport();
        $grid->disableRowSelector();


        $grid->filter(function (Grid\Filter $filter) {
            $filter->expand();
            // 去掉默认的id过滤器
            $filter->disableIdFilter();

            $filter->column(1/3, function ($filter) {
                $filter->like('name', '姓名');
            });

            $filter->column(1/3, function ($filter) {
                $filter->equal('org_id', '所属单位')->select(Org::where('type', 0)->pluck('title', 'id'));
            });

        });

        $grid->setActionClass(Actions::class);

        $grid->actions(function ($actions) {
            // 去掉查看
            $actions->disableView();
        });

        return $grid;
    }

    protected function detail($id)
    {
        $show = new Show(Member::findOrFail($id));

        $show->field('name', '姓名');
        $show->field('mobile', '手机号');
        $show->field('pos', '职位');
        $show->field('status', '状态');

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Member());

        $form->text('name', '姓名')->rules('required');
        $form->text('mobile', '手机号')->creationRules(['required', "unique:members"])->updateRules(['required', "unique:members,mobile,{{id}}"]);

        $form->password('pwd', '密码')->rules('required');
        $form->text('pos', '职位');
        $form->select('org_id', '权限范围')->options(Org::where('type', 0)->pluck('title', 'id'))->rules('required');
        $form->radio('status', '状态')->options(['0' => '禁用', '1' => '启用'])->default('1');

        $form->saved(function (Form $form) {
            Member::where('id', $form->model()->id)->update([
                'pwd' => md5($form->model()->pwd),
            ]);

            if($form->isCreating()){
                Work::create([
                    'member_id' => $form->model()->id,
                    'org_id' => $form->model()->org_id,
                    'year' => date('Y'),
                    'target' => 4,
                ]);
            }
        });

        $form->footer(function ($footer) {
            // 去掉`查看`checkbox
            $footer->disableViewCheck();
            // 去掉`继续编辑`checkbox
            $footer->disableEditingCheck();
            // 去掉`继续创建`checkbox
            $footer->disableCreatingCheck();

        });

        $form->tools(function (Form\Tools $tools) {
            // 去掉`列表`按钮
            $tools->disableList();
            // 去掉`删除`按钮
            $tools->disableDelete();
            // 去掉`查看`按钮
            $tools->disableView();
        });

        return $form;
    }
}
